home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: artemis.sto.fdata.se!news
- From: Niklas Mellin <niklas.mellin@sto.fdata.se>
- Subject: Re: beginner question - typecasting
- Sender: news@artemis.sto.fdata.se (UseNet NetNews)
- Message-ID: <30EBCED7.774@sto.fdata.se>
- Date: Thu, 4 Jan 1996 12:57:59 GMT
- Content-Transfer-Encoding: 7bit
- Content-Type: text/plain; charset=us-ascii
- References: <4cei1r$s02@sun.cis.smu.edu>
- Mime-Version: 1.0
- X-Mailer: Mozilla 2.0b4 (WinNT; I)
- Organization: WM-data F÷rsvarsdata AB, Sweden
-
- Damon Bowman wrote:
- >
- > When you are typecasting, is there any difference between:
- >
- > a = int(x)
- > and
- > a = (int) x
-
- [...]
-
- Yes the first is not type casting, but a "type conversion".
-
- When x is a variables of built in types there is no
- difference. But if x is a varible of some class for instance
- the first form generates a call to that class' operator int(),
- generating a compile time error message if there is no such
- member function.
-
- The 2nd form is an inheritance from plain C, and doesn't work
- when x is of class type. It is also
- not "safe", meaning that the compiler will not generate error
- messages in certain situations, and you will get buggy code
- or run time errors instead.
-
- In general u should choose the conversion form if possible,
- but sometimes a type cast can be handy. One case is if you
- are using a 3rd part class library that is not well written
- (read MFC) and they forgot to declare function parameters as
- const and you have a const object you would like to pass to
- that function. Then you can type cast your object or variable
- to non const.
-
- ---
- Niklas Mellin
-